package com.zjt.takeoutproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjt.takeoutproject.common.BaseContext;
import com.zjt.takeoutproject.common.R;
import com.zjt.takeoutproject.entity.ShoppingCart;
import com.zjt.takeoutproject.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-07-09
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 查询指定用户的购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", shoppingCart);

        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != shoppingCart.getUserId(), ShoppingCart::getUserId, shoppingCart.getUserId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        //SQL:select * from Shopping_Cart where user_id = ? order by Create_Time desc
        return R.success(shoppingCartService.list(queryWrapper));
    }

    //新增购物车商品
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);

        //指定是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前菜品或套餐是否已存在购物车中
        Long dishId = shoppingCart.getDishId();
        //通过用户id和（套餐id）菜品id和口味来判断
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加指定用户id为条件
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
            //添加的是菜品id，判断该菜品购物车里是否存在
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
//            //判断该是否为同一种口味
//            if (shoppingCart.getDishFlavor()!=null){
//                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
//            }
        } else {
            //添加的是套餐id
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //判断添加的id购物车里是否存在，存在则在原数量上增1，不存在则添加到购物车
        ShoppingCart cartServiceOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (cartServiceOne != null){
            Integer cartServiceOneNumber = cartServiceOne.getNumber();
            cartServiceOne.setNumber(cartServiceOneNumber+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }

        return R.success(cartServiceOne);
    }

    //减少购物车物品
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);
        //指定是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();

        //通过用户id和（套餐id）菜品id来获取指定
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加指定用户id为条件
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
            //删除的是菜品id，判断该菜品购物车里是否存在
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
//            //判断该是否为同一种口味
//            if (shoppingCart.getDishFlavor()!=null){
//                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
//            }
        } else {
            //删除的是套餐id
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        log.info("cartServiceOne:{}",cartServiceOne.toString());
        if (cartServiceOne.getNumber()>1){
            //如果已经存在，就在原来数量基础上减去一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number-1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        }
        return R.success(cartServiceOne);
    }

    //清空购物车内容
    @DeleteMapping("/clean")
    public R<String> clean(){
        //delete from shopping_cart where user_id = ?
//        Long currentId = BaseContext.getCurrentId();
//        LambdaQueryWrapper<ShoppingCart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        cartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
//        shoppingCartService.remove(cartLambdaQueryWrapper);
        shoppingCartService.clean();

        return R.success("清空购物车成功");
    }
}
